package cn.edu.controller;

import cn.edu.DAO.UserDAOByXML;
import cn.edu.DO.PlaceDO;
import cn.edu.DO.UserDO;
import cn.edu.DO.UserplayDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserDAOByXML userDAO;

    @RequestMapping("/getalluserplay")
    public List<UserplayDO> getalluserplay() {
        return userDAO.selectAlluserplay();
    }

    @RequestMapping("/tologin")
    public String toLogin() {
        return "user/login";
    }
    @RequestMapping("/toadduser")
    public String toadduser() {
        return "user/adduser";
    }

    @RequestMapping("/addUser")
    public String addtrip(HttpServletRequest Request, Map map){
        String username =Request.getParameter("username");
        String password =Request.getParameter("password");
        if (userDAO.insertuser(username,password)!=null){
            map.put("insertright",("注册成功"));
            return "index";
        }
        else{
            map.put("inserterror",("用户名已存在"));
            return("index");
        }
    }


    @RequestMapping("/loginCheck")
    public String loginCheck(UserDO user, Map map, HttpSession session) {
        if (userDAO.queryuserToLogin(user.getUsername(),user.getPassword()) != null) {
            map.put("username", user.getUsername());
            session.setAttribute("username", user.getUsername());
            return "index";
        } else {
            map.put("error", "用户名或者密码错误");
            return "user/login";
        }
    }

    @RequestMapping("/getUserlist")
    public String getUserList(Map map){
        //Map,request.setSttribute,Model
        List<UserplayDO> userplaylist = userDAO.selectAlluserplay();
        map.put("userplaylist",userplaylist);
        return "user/userlist";
    }



}